package com.jiamao.goplay.ui.settings.vm;

import android.app.Application;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.jiamao.goplay.config.Config;
import com.jiamao.goplay.data.DataRepository;
import com.jiamao.goplay.ui.settings.AboutFragment;
import com.jiamao.goplay.ui.settings.ColorFragment;
import com.jiamao.goplay.ui.settings.FeedBackFragment;
import com.jiamao.goplay.ui.settings.NewMessageRemindFragment;
import com.jiamao.goplay.ui.settings.SettingsFragment;
import com.jiamao.goplay.ui.web.WebFragment;
import com.jiamao.goplay.util.DataCleanManager;
import com.tamsiree.rxkit.RxAppTool;
import com.tamsiree.rxkit.view.RxToast;

import java.util.Objects;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.SPUtils;

public class SettingsVM extends BaseViewModel<DataRepository> {

    //用户名的绑定
    public ObservableField<String> openStatus = new ObservableField<>("已开启");
    public ObservableField<String> clearSize = new ObservableField<>("");
    public ObservableField<String> version = new ObservableField<>("");
    //封装一个界面发生改变的观察者
    public SettingsVM.UIChangeObservable uc = new SettingsVM.UIChangeObservable();

    public class UIChangeObservable {
        //密码开关观察者
        public SingleLiveEvent<Boolean> logout = new SingleLiveEvent<>();
        public SingleLiveEvent<Boolean> clearCache = new SingleLiveEvent<>();
        public SingleLiveEvent<String> weChat = new SingleLiveEvent<>();
    }

    public SettingsVM(@NonNull Application application, DataRepository repository) {
        super(application, repository);

        String totalCacheSize = DataCleanManager.getTotalCacheSize(Objects.requireNonNull(getApplication()));
        clearSize.set(totalCacheSize);
        version.set("玩多多版本号："+RxAppTool.getAppVersionName(getApplication()));

        KLog.d("aaaaaa:"+SPUtils.getInstance().getBoolean(Config.APP_SETINGS_TZ,true));
        if (SPUtils.getInstance().getBoolean(Config.APP_SETINGS_TZ,true)){
            openStatus.set("已开启");
        }else {
            openStatus.set("未开启");
        }
       // SPUtils.getInstance().getBoolean(Config.APP_SETINGS_TZ)?openStatus.set("已开启"):openStatus.set("未开启");


    }
    //登录按钮的点击事件
    public BindingCommand backOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            finish();
        }
    });


    //登录按钮的点击事件
    public BindingCommand copeyCommd = new BindingCommand(new BindingAction() {
        @Override
        public void call() {

            uc.weChat.setValue("13810036443");

           // copyContentToClipboard(getApplication(),"13810036443");
        }
    });

    /**
     * 复制内容到剪贴板
     *
     * @param content
     * @param context
     */
    public void copyContentToClipboard(String content, Context context) {
        //获取剪贴板管理器：
        ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        // 创建普通字符型ClipData
        ClipData mClipData = ClipData.newPlainText("Label", content);
        // 将ClipData内容放到系统剪贴板里。
        cm.setPrimaryClip(mClipData);
    }

    //登录按钮的点击事件
    public BindingCommand appVersionCommd = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
       //     RxToast.showToastShort("检测更新");

        }
    });

    public BindingCommand colorCommd = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            startContainerActivity(ColorFragment.class.getCanonicalName());
        }
    });

    //登录按钮的点击事件
    public BindingCommand wechatCommd = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
         //   RxToast.showToastShort("复制微信-夜间");

        }
    });

    //登录按钮的点击事件
    public BindingCommand clearCacheCommd = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            uc.clearCache.setValue(true);
        }
    });

    //登录按钮的点击事件
    public BindingCommand newmsgredminCommd = new BindingCommand(new BindingAction() {
        @Override
        public void call() {

              startContainerActivity(NewMessageRemindFragment.class.getCanonicalName());
        }
    });

    //登录按钮的点击事件
    public BindingCommand aboutCommd = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Bundle bd = new Bundle();
            bd.putString("url", Config.WEB_YHXY);
            bd.putString("title","用户协议");
            startContainerActivity(WebFragment.class.getCanonicalName(),bd);
             //  startContainerActivity(AboutFragment.class.getCanonicalName());
        }
    });

    //登录按钮的点击事件
    public BindingCommand tkCommd = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Bundle bd = new Bundle();
            bd.putString("url", Config.WEB_YSZC);
            bd.putString("title","隐私条款");
            startContainerActivity(WebFragment.class.getCanonicalName(),bd);
            //  startContainerActivity(AboutFragment.class.getCanonicalName());
        }
    });

    //登录按钮的点击事件
    public BindingCommand logoutCommd = new BindingCommand(new BindingAction() {
        @Override
        public void call() {

            uc.logout.setValue(true);
//            RxToast.showToastShort("退出登录");
//            finish();
           // startContainerActivity(AboutFragment.class.getCanonicalName());
        }
    });

    //跳转意见反馈
    public BindingCommand feedBackCommd = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            RxToast.showToastShort("敬请期待");
           // startContainerActivity(FeedBackFragment.class.getCanonicalName());
        }
    });


    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
